// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log('登录成功', res)
      }
    })

    // 初始化蓝牙权限检查
    this.checkBluetoothPermission()
  },
  onShow() {
    console.log('App Show')
  },
  onHide() {
    console.log('App Hide')
  },
  globalData: {
    userInfo: null,
    bluetoothInitialized: false
  },

  // 检查蓝牙权限
  async checkBluetoothPermission() {
    try {
      const res = await wx.getSetting()
      if (!res.authSetting['scope.bluetooth']) {
        // 如果没有蓝牙权限，请求权限
        await wx.authorize({
          scope: 'scope.bluetooth'
        })
      }
      this.globalData.bluetoothInitialized = true
    } catch (error) {
      console.error('蓝牙权限检查失败:', error)
    }
  }
})
